// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/*
 * @Author: qiulin 1746365080@qq.com
 * @Date: 2024-10-24 15:35:51
 * @LastEditors: qiulin_JXGM ql1746365080@gmail.com
 * @LastEditTime: 2024-12-06 19:59:30
 * @FilePath: /SGSim/DBManager/UnitTest/HDF5/Input/LoadTest.cpp
 */

#include <vector>

#include <gtest/gtest.h>

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Load/ICONVService.h"
#include "DBManager/IService/Input/Load/IFORCE1Service.h"
#include "DBManager/IService/Input/Load/IFORCE2Service.h"
#include "DBManager/IService/Input/Load/IFORCEService.h"
#include "DBManager/IService/Input/Load/IGRAVService.h"
#include "DBManager/IService/Input/Load/ILoadCombinationService.h"
#include "DBManager/IService/Input/Load/IMOMENT1Service.h"
#include "DBManager/IService/Input/Load/IMOMENT2Service.h"
#include "DBManager/IService/Input/Load/IMOMENTService.h"
#include "DBManager/IService/Input/Load/IPLOAD1Service.h"
#include "DBManager/IService/Input/Load/IPLOAD2Service.h"
#include "DBManager/IService/Input/Load/IPLOAD4Service.h"
#include "DBManager/IService/Input/Load/IPLOADService.h"
#include "DBManager/IService/Input/Load/IQBDY3Service.h"
#include "DBManager/IService/Input/Load/ISPCDService.h"
#include "DBManager/UnitTest/HDF5/TestDB.h"

using namespace SG::DBManager;

using namespace SG::DataStructure::FEM;
using namespace SG::DBManager;

class I_LoadTest : public testing::Test
{
  public:
    static void SetUpTestSuite ()
    {
        if (DB::ServiceFactory != nullptr)
        {
            DB::ServiceFactory->ClearDatabase ();
        }
    }
};

TEST_F (I_LoadTest, GRAVData)
{
    SG::DataStructure::Common::Id const id = 99;

    // 创建对象
    GRAVData obj;
    obj.m_sId = id;

    // 获取服务
    auto service = DB::ServiceFactory->get<IGRAVService> ();
    // 添加对象
    service->Add (obj);

    ASSERT_EQ (service->Size (), 1);
    ASSERT_EQ (service->FindById (id, obj), SG::DataStructure::Common::Status::STATUS_Success);
    ASSERT_EQ (obj.m_sId, id);

    // GetGravitySet
    ASSERT_EQ (service->GetGravitySet ().size (), 1);
}

TEST_F (I_LoadTest, FORCEData)
{
    SG::DataStructure::Common::Id const loadcaseId = 99;

    // 创建对象
    FORCEData obj;
    obj.m_sId = loadcaseId;

    // 获取服务
    auto service = DB::ServiceFactory->get<IFORCEService> ();
    // 添加对象
    service->Add (obj);

    ASSERT_EQ (service->Size (), 1);

    std::vector<FORCEData> results;
    // FindBySId
    service->FindBySId (loadcaseId, results);
    ASSERT_EQ (results.size (), 1);
    ASSERT_EQ (results[0].m_sId, loadcaseId);
}

TEST_F (I_LoadTest, MOMENTData)
{
    SG::DataStructure::Common::Id const loadcaseId = 99;

    // 创建对象
    MOMENTData obj;
    obj.m_sId = loadcaseId;

    // 获取服务
    auto service = DB::ServiceFactory->get<IMOMENTService> ();
    // 添加对象
    service->Add (obj);

    ASSERT_EQ (service->Size (), 1);

    std::vector<MOMENTData> results;
    // FindBySId
    service->FindBySId (loadcaseId, results);
    ASSERT_EQ (results.size (), 1);
    ASSERT_EQ (results[0].m_sId, loadcaseId);
}

TEST_F (I_LoadTest, FORCE1Data)
{
    SG::DataStructure::Common::Id const loadcaseId = 99;

    // 创建对象
    FORCE1Data obj;
    obj.m_sId = loadcaseId;

    // 获取服务
    auto service = DB::ServiceFactory->get<IFORCE1Service> ();
    // 添加对象
    service->Add (obj);

    ASSERT_EQ (service->Size (), 1);

    std::vector<FORCE1Data> results;
    // FindBySId
    service->FindBySId (loadcaseId, results);
    ASSERT_EQ (results.size (), 1);
    ASSERT_EQ (results[0].m_sId, loadcaseId);
}

TEST_F (I_LoadTest, FORCE2Data)
{
    SG::DataStructure::Common::Id const loadcaseId = 99;

    // 创建对象
    FORCE2Data obj;
    obj.m_sId = loadcaseId;

    // 获取服务
    auto service = DB::ServiceFactory->get<IFORCE2Service> ();
    // 添加对象
    service->Add (obj);

    ASSERT_EQ (service->Size (), 1);

    std::vector<FORCE2Data> results;
    // FindBySId
    service->FindBySId (loadcaseId, results);
    ASSERT_EQ (results.size (), 1);
    ASSERT_EQ (results[0].m_sId, loadcaseId);
}

TEST_F (I_LoadTest, MOMENT1Data)
{
    SG::DataStructure::Common::Id const loadcaseId = 99;

    // 创建对象
    MOMENT1Data obj;
    obj.m_sId = loadcaseId;

    // 获取服务
    auto service = DB::ServiceFactory->get<IMOMENT1Service> ();
    // 添加对象
    service->Add (obj);

    ASSERT_EQ (service->Size (), 1);

    std::vector<MOMENT1Data> results;
    // FindBySId
    service->FindBySId (loadcaseId, results);
    ASSERT_EQ (results.size (), 1);
    ASSERT_EQ (results[0].m_sId, loadcaseId);
}

TEST_F (I_LoadTest, MOMENT2Data)
{
    SG::DataStructure::Common::Id const loadcaseId = 99;

    // 创建对象
    MOMENT2Data obj;
    obj.m_sId = loadcaseId;

    // 获取服务
    auto service = DB::ServiceFactory->get<IMOMENT2Service> ();
    // 添加对象
    service->Add (obj);

    ASSERT_EQ (service->Size (), 1);

    std::vector<MOMENT2Data> results;
    // FindBySId
    service->FindBySId (loadcaseId, results);
    ASSERT_EQ (results.size (), 1);
    ASSERT_EQ (results[0].m_sId, loadcaseId);
}

TEST_F (I_LoadTest, LoadCombinationDatatest)
{
    SG::DataStructure::Common::Id const loadcaseId = 55;

    // 创建对象
    LoadCombination obj;
    obj.m_sId     = loadcaseId;
    obj.m_facotor = 1.0;
    obj.m_Items   = { { 1.0, 1 }, { 2.0, 2 } };
    // 获取服务
    auto service = DB::ServiceFactory->get<ILoadCombinationService> ();
    // 添加对象
    service->Add (obj);

    ASSERT_EQ (service->Size (), 1);
}

TEST_F (I_LoadTest, PLOADDatatest)
{
    SG::DataStructure::Common::Id const loadcaseId = 55;

    // 创建对象
    PLOADData obj;
    obj.m_sId  = loadcaseId;
    obj.m_P    = 0.5;
    obj.m_G[0] = 15;
    obj.m_G[1] = 14;
    obj.m_G[2] = 12;
    obj.m_G[3] = 11;

    // 获取服务
    auto service = DB::ServiceFactory->get<IPLOADService> ();
    // 添加对象
    service->Add (obj);

    ASSERT_EQ (service->Size (), 1);
}

TEST_F (I_LoadTest, PLOAD1Datatest)
{
    SG::DataStructure::Common::Id const loadcaseId = 55;

    // 创建对象
    PLOAD1Data obj;
    obj.m_sId = loadcaseId;
    obj.m_p1  = 0.5;

    // 获取服务
    auto service = DB::ServiceFactory->get<IPLOAD1Service> ();
    // 添加对象
    service->Add (obj);

    ASSERT_EQ (service->Size (), 1);
}

TEST_F (I_LoadTest, PLOAD2Datatest)
{
    SG::DataStructure::Common::Id const loadcaseId = 55;

    // 创建对象
    PLOAD2Data obj;
    obj.m_sId = loadcaseId;

    // 获取服务
    auto service = DB::ServiceFactory->get<IPLOAD2Service> ();
    // 添加对象
    service->Add (obj);

    ASSERT_EQ (service->Size (), 1);
}

TEST_F (I_LoadTest, PLOAD4Datatest)
{
    SG::DataStructure::Common::Id const loadcaseId = 55;

    // 创建对象
    PLOAD4Data obj;
    obj.m_sId = loadcaseId;

    // 获取服务
    auto service = DB::ServiceFactory->get<IPLOAD4Service> ();
    // 添加对象
    service->Add (obj);

    ASSERT_EQ (service->Size (), 1);
}

TEST_F (I_LoadTest, SPCDData)
{
    // 创建对象
    SPCDData obj;
    obj.m_sId    = 99;
    auto service = DB::ServiceFactory->get<ISPCDService> ();
    for (auto i = 0; i < 10; i++)
    {
        obj.m_g = i;
        service->Add (obj);
    }

    for (auto i = 0; i < 10; i++)
    {
        obj.m_sId = i;
        service->Add (obj);
    }
    // 获取服务

    // 添加对象
    std::vector<SPCDData> findData;

    ASSERT_EQ (service->FindBySId (11, findData), SG::DataStructure::Common::Status::STATUS_Failure);
    ASSERT_EQ (service->FindBySId (1, findData), SG::DataStructure::Common::Status::STATUS_Success);

    ASSERT_EQ (findData.size (), 1);
    ASSERT_EQ (findData[0].m_sId, 1);

    findData.clear ();
    ASSERT_EQ (service->FindBySId (99, findData), SG::DataStructure::Common::Status::STATUS_Success);
    ASSERT_EQ (findData.size (), 10);
    ASSERT_EQ (findData[5].m_g, 5);
}

TEST_F (I_LoadTest, QBDY3Datatest)
{
    SG::DataStructure::Common::Id const loadcaseId = 55;

    // 创建对象
    QBDY3Data obj;
    obj.m_sId = loadcaseId;

    // 获取服务
    auto service = DB::ServiceFactory->get<IQBDY3Service> ();
    // 添加对象
    service->Add (obj);

    ASSERT_EQ (service->Size (), 1);
}

TEST_F (I_LoadTest, CONVDatatest)
{
    DB::ServiceFactory->ClearDatabase ();
    // 获取服务
    auto service = DB::ServiceFactory->get<ICONVService> ();

    SG::DataStructure::Common::Id const id = 55;

    // 创建对象
    CONVData obj;
    obj.m_id = 1;
    ASSERT_EQ (service->Add (obj), SG::DataStructure::Common::Status::STATUS_Success);

    // 创建对象
    obj.m_id = id;

    // 添加对象
    ASSERT_EQ (service->Add (obj), SG::DataStructure::Common::Status::STATUS_Success);

    CONVData _OUT outData;
    ASSERT_EQ (service->FindById (id, outData), SG::DataStructure::Common::Status::STATUS_Success);

    ASSERT_EQ (outData.m_id, id);
    // ASSERT_EQ (outData.size (), 1);
    ASSERT_EQ (service->Size (), 2);
}

#include "DBManager/IService/Input/Load/IDAREAService.h"
TEST_F (I_LoadTest, DAREADatatest)
{
    DB::ServiceFactory->ClearDatabase ();
    // 获取服务
    auto service = DB::ServiceFactory->get<IDAREAService> ();

    SG::DataStructure::Common::Id const id = 55;

    // 创建对象
    DAREAData obj;
    obj.m_sId = 1;
    ASSERT_EQ (service->Add (obj), SG::DataStructure::Common::Status::STATUS_Success);

    // 创建对象
    obj.m_sId = id;

    // 添加对象
    ASSERT_EQ (service->Add (obj), SG::DataStructure::Common::Status::STATUS_Success);

    std::vector<DAREAData> _OUT outData;
    ASSERT_EQ (service->FindBySId (id, outData), SG::DataStructure::Common::Status::STATUS_Success);

    ASSERT_EQ (outData.size (), 1);
    ASSERT_EQ (service->Size (), 2);
}

#include "DBManager/IService/Input/Load/IDELAYService.h"
TEST_F (I_LoadTest, DELAYDatatest)
{
    DB::ServiceFactory->ClearDatabase ();
    // 获取服务
    auto service = DB::ServiceFactory->get<IDELAYService> ();

    SG::DataStructure::Common::Id const id = 55;

    // 创建对象
    DELAYData obj;
    obj.m_sId = 1;
    ASSERT_EQ (service->Add (obj), SG::DataStructure::Common::Status::STATUS_Success);

    // 创建对象
    obj.m_sId = id;

    // 添加对象
    ASSERT_EQ (service->Add (obj), SG::DataStructure::Common::Status::STATUS_Success);

    std::vector<DELAYData> _OUT outData;
    ASSERT_EQ (service->FindBySId (id, outData), SG::DataStructure::Common::Status::STATUS_Success);

    ASSERT_EQ (outData.size (), 1);
    ASSERT_EQ (service->Size (), 2);
}

#include "DBManager/IService/Input/Load/IDPHASEService.h"
TEST_F (I_LoadTest, DPHASEDatatest)
{
    DB::ServiceFactory->ClearDatabase ();
    // 获取服务
    auto service = DB::ServiceFactory->get<IDPHASEService> ();

    SG::DataStructure::Common::Id const id = 55;

    // 创建对象
    DPHASEData obj;
    obj.m_sId = 1;
    ASSERT_EQ (service->Add (obj), SG::DataStructure::Common::Status::STATUS_Success);

    // 创建对象
    obj.m_sId = id;

    // 添加对象
    ASSERT_EQ (service->Add (obj), SG::DataStructure::Common::Status::STATUS_Success);

    std::vector<DPHASEData> _OUT outData;
    ASSERT_EQ (service->FindBySId (id, outData), SG::DataStructure::Common::Status::STATUS_Success);

    ASSERT_EQ (outData.size (), 1);
    ASSERT_EQ (service->Size (), 2);
}

#include "DBManager/IService/Input/Load/ILSEQService.h"
TEST_F (I_LoadTest, LSEQDatatest)
{
    DB::ServiceFactory->ClearDatabase ();
    // 获取服务
    auto service = DB::ServiceFactory->get<ILSEQService> ();

    SG::DataStructure::Common::Id const id = 55;

    // 创建对象
    LSEQData obj;
    obj.m_sId = 1;
    obj.m_DAREA = 3;
    obj.m_LID = 6;
    ASSERT_EQ (service->Add (obj), SG::DataStructure::Common::Status::STATUS_Success);

    // 创建对象
    obj.m_sId = id;
    obj.m_DAREA = 4;
    obj.m_LID = 7;

    // 添加对象
    ASSERT_EQ (service->Add (obj), SG::DataStructure::Common::Status::STATUS_Success);

    std::vector<LSEQData> _OUT outData;
    ASSERT_EQ (service->FindBySId (id, outData), SG::DataStructure::Common::Status::STATUS_Success);

    ASSERT_EQ (outData.size (), 1);
    ASSERT_EQ (service->Size (), 2);

    LSEQData lseq;
    ASSERT_EQ (service->FindByLoadId(1, 3, lseq), SG::DataStructure::Common::Status::STATUS_Success);
    ASSERT_EQ (lseq.m_LID, 6);

}

#include "DBManager/IService/Input/Load/IRLOAD1Service.h"
TEST_F (I_LoadTest, RLOAD1Datatest)
{
    DB::ServiceFactory->ClearDatabase ();
    // 获取服务
    auto service = DB::ServiceFactory->get<IRLOAD1Service> ();

    SG::DataStructure::Common::Id const id = 55;

    // 创建对象
    RLOAD1Data obj;
    obj.m_sId = 1;
    ASSERT_EQ (service->Add (obj), SG::DataStructure::Common::Status::STATUS_Success);

    // 创建对象
    obj.m_sId = id;

    // 添加对象
    ASSERT_EQ (service->Add (obj), SG::DataStructure::Common::Status::STATUS_Success);

    std::vector<RLOAD1Data> _OUT outData;
    ASSERT_EQ (service->FindBySId (id, outData), SG::DataStructure::Common::Status::STATUS_Success);

    ASSERT_EQ (outData.size (), 1);
    ASSERT_EQ (service->Size (), 2);
}

#include "DBManager/IService/Input/Load/IRLOAD2Service.h"
TEST_F (I_LoadTest, RLOAD2Datatest)
{
    DB::ServiceFactory->ClearDatabase ();
    // 获取服务
    auto service = DB::ServiceFactory->get<IRLOAD2Service> ();

    SG::DataStructure::Common::Id const id = 55;

    // 创建对象
    RLOAD2Data obj;
    obj.m_sId = 1;
    ASSERT_EQ (service->Add (obj), SG::DataStructure::Common::Status::STATUS_Success);

    // 创建对象
    obj.m_sId = id;

    // 添加对象
    ASSERT_EQ (service->Add (obj), SG::DataStructure::Common::Status::STATUS_Success);

    std::vector<RLOAD2Data> _OUT outData;
    ASSERT_EQ (service->FindBySId (id, outData), SG::DataStructure::Common::Status::STATUS_Success);

    ASSERT_EQ (outData.size (), 1);
    ASSERT_EQ (service->Size (), 2);
}

#include "DBManager/IService/Input/Load/ISLOADService.h"
TEST_F (I_LoadTest, SLOADDatatest)
{
    DB::ServiceFactory->ClearDatabase ();
    // 获取服务
    auto service = DB::ServiceFactory->get<ISLOADService> ();

    SG::DataStructure::Common::Id const id = 55;

    // 创建对象
    SLOADData obj;
    obj.m_sId = 1;
    ASSERT_EQ (service->Add (obj), SG::DataStructure::Common::Status::STATUS_Success);

    // 创建对象
    obj.m_sId = id;

    // 添加对象
    ASSERT_EQ (service->Add (obj), SG::DataStructure::Common::Status::STATUS_Success);

    std::vector<SLOADData> _OUT outData;
    ASSERT_EQ (service->FindBySId (id, outData), SG::DataStructure::Common::Status::STATUS_Success);

    ASSERT_EQ (outData.size (), 1);
    ASSERT_EQ (service->Size (), 2);
}

#include "DBManager/IService/Input/Load/ITLOAD1Service.h"
TEST_F (I_LoadTest, TLOAD1Datatest)
{
    DB::ServiceFactory->ClearDatabase ();
    // 获取服务
    auto service = DB::ServiceFactory->get<ITLOAD1Service> ();

    SG::DataStructure::Common::Id const id = 55;

    // 创建对象
    TLOAD1Data obj;
    obj.m_sId = 1;
    ASSERT_EQ (service->Add (obj), SG::DataStructure::Common::Status::STATUS_Success);

    // 创建对象
    obj.m_sId = id;

    // 添加对象
    ASSERT_EQ (service->Add (obj), SG::DataStructure::Common::Status::STATUS_Success);

    std::vector<TLOAD1Data> _OUT outData;
    ASSERT_EQ (service->FindBySId (id, outData), SG::DataStructure::Common::Status::STATUS_Success);

    ASSERT_EQ (outData.size (), 1);
    ASSERT_EQ (service->Size (), 2);
}

#include "DBManager/IService/Input/Load/ITLOAD2Service.h"
TEST_F (I_LoadTest, TLOAD2Datatest)
{
    // 获取服务
    auto service = DB::ServiceFactory->get<ITLOAD2Service> ();

    SG::DataStructure::Common::Id const id = 54;

    // 创建对象
    TLOAD2Data obj;
    obj.m_sId = 1;
    ASSERT_EQ (service->Add (obj), SG::DataStructure::Common::Status::STATUS_Success);

    // 创建对象
    obj.m_sId = id;

    // 添加对象
    ASSERT_EQ (service->Add (obj), SG::DataStructure::Common::Status::STATUS_Success);

    std::vector<TLOAD2Data> _OUT outData;
    ASSERT_EQ (service->FindBySId (id, outData), SG::DataStructure::Common::Status::STATUS_Success);

    ASSERT_EQ (outData.size (), 1);
    ASSERT_EQ (service->Size (), 2);
}

#include "DBManager/IService/Input/Load/IDLOADService.h"
TEST_F (I_LoadTest, DLOADDatatest)
{
    // 获取服务
    auto service = DB::ServiceFactory->get<IDLOADService> ();

    SG::DataStructure::Common::Id const id = 56;

    // 创建对象
    DLOADData obj;
    obj.m_sId    = 1;
    obj.m_SLList = { { 1, 3 } };
    ASSERT_EQ (service->Add (obj), SG::DataStructure::Common::Status::STATUS_Success);

    // 创建对象
    obj.m_sId = id;

    // 添加对象
    ASSERT_EQ (service->Add (obj), SG::DataStructure::Common::Status::STATUS_Success);

    DLOADData _OUT outData;
    ASSERT_EQ (service->FindBySId (id, outData), SG::DataStructure::Common::Status::STATUS_Success);

    // ASSERT_EQ (outData.size (), 1);
    ASSERT_EQ (service->Size (), 2);
}

#include "DBManager/IService/Input/Load/ILoadSummaryService.h"
TEST_F (I_LoadTest, LoadSummary)
{
    // 创建对象
    GRAVData obj;
    obj.m_sId = 99;
    obj.m_cId = 123;

    // 获取服务
    auto service = DB::ServiceFactory->get<IGRAVService> ();
    // 添加对象
    service->Add (obj);

    obj.m_sId = 88;
    obj.m_cId = 321;
    service->Add (obj);

    ASSERT_EQ (service->Size (), 2);

    // 获取服务
    auto sumService = DB::ServiceFactory->get<ILoadSummaryService> ();

    auto types = sumService->GetLoadType (55);

    ASSERT_EQ (types.size (), 1);
    ASSERT_EQ (*types.begin (), LoadType::LOAD_TLOAD1);

    auto gravDatas = sumService->FindGRAV ({ 99, 88, 55, 66 });

    ASSERT_EQ (gravDatas.size (), 2);

    ASSERT_EQ(gravDatas[1].m_sId, 99);
    ASSERT_EQ(gravDatas[0].m_sId, 88);
    ASSERT_EQ(gravDatas[1].m_cId, 123);
    ASSERT_EQ(gravDatas[0].m_cId, 321);
}